package edu.guigu.servicedemo.aidl;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import edu.guigu.servicedemo.IRemoteAidl;
import edu.guigu.servicedemo.MyData;

public class MyAiidlService extends Service {
    private MyAidlBinder myAidlBinder;
    public MyAiidlService() {}
    @Override
    public IBinder onBind(Intent intent) {
        if(myAidlBinder == null){
            myAidlBinder = new MyAidlBinder();
        }
        return myAidlBinder;
    }

    // 创建Binder子类,@see IRemoteAidl.Stub是Binder的子类,AIDL接口的业务实现类
    class MyAidlBinder extends IRemoteAidl.Stub {

        @Override
        public MyData getMyData() throws RemoteException {
            MyData myData = new MyData(100, "Hello "+Math.random()*100);
            return myData;
        }

        // 执行的方法(实现此接口，根据type去执行不同的动作业务)
        @Override
        public void action(int type) throws RemoteException {
            switch (type){
                case 1:
                    action1_start();
                    break;
                case 2:
                    action2_pause();
                    break;
                case 3:
                    action3_stop();
                    break;
            }
        }


        private void action1_start(){
            Log.d("TAG", "action1_start: ");
        }
        private void action2_pause(){
            Log.d("TAG", "action2_pause: ");
        }
        private void action3_stop(){
            Log.d("TAG", "action3_stop: ");
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        myAidlBinder = null;
    }
}